// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Sum.java

package finance;

import include.nseer_db.nseer_db;
import java.sql.ResultSet;

public class Sum
{

	private int count;
	private int real_count;
	private int result[];
	private String file_id[];
	private int parent_category_id[];
	private int category_id[];
	private String details_tag[];
	private String file_name[];
	private double debit_sum[];
	private double loan_sum[];
	private int debit_count[];
	private int loan_count[];
	private double current_balance_sum[];
	private double last_balance_sum[];
	private double debit_sum_all;
	private double loan_sum_all;
	private double current_balance_sum_all;
	private double last_balance_sum_all;
	private int debit_count_all;
	private int loan_count_all;

	public Sum()
	{
		count = 0;
		real_count = 0;
		result = new int[1];
		file_id = new String[1];
		parent_category_id = new int[1];
		category_id = new int[1];
		details_tag = new String[1];
		file_name = new String[1];
		debit_sum = new double[1];
		loan_sum = new double[1];
		debit_count = new int[1];
		loan_count = new int[1];
		current_balance_sum = new double[1];
		last_balance_sum = new double[1];
		debit_sum_all = 0.0D;
		loan_sum_all = 0.0D;
		current_balance_sum_all = 0.0D;
		last_balance_sum_all = 0.0D;
		debit_count_all = 0;
		loan_count_all = 0;
	}

	public void calculate(nseer_db finance_db, String timea)
	{
		try
		{
			String sql = "select category_id from (finance_config_file_kind where file_ID like '1001%' or bank_tag='1') order by category_id desc";
			ResultSet rs = finance_db.executeQuery(sql);
			if (rs.next())
				count = rs.getInt("category_id") + 1;
			result = new int[count];
			file_id = new String[count];
			parent_category_id = new int[count];
			category_id = new int[count];
			details_tag = new String[count];
			file_name = new String[count];
			debit_sum = new double[count];
			loan_sum = new double[count];
			debit_count = new int[count];
			loan_count = new int[count];
			current_balance_sum = new double[count];
			last_balance_sum = new double[count];
			sql = "select file_id,file_name,parent_category_id,category_id,details_tag from (finance_config_file_kind where file_ID like '1001%' or bank_tag='1') order by file_ID";
			for (rs = finance_db.executeQuery(sql); rs.next();)
			{
				file_id[rs.getInt("category_id")] = rs.getString("file_id");
				file_name[rs.getInt("category_id")] = rs.getString("file_name");
				parent_category_id[rs.getInt("category_id")] = rs.getInt("parent_category_id");
				category_id[rs.getInt("category_id")] = rs.getInt("category_id");
				details_tag[rs.getInt("category_id")] = rs.getString("details_tag");
				result[real_count] = rs.getInt("category_id");
				real_count++;
			}

			String account_period = "";
			sql = (new StringBuilder("select account_period from finance_account_period where start_time<='")).append(timea).append("' and end_time>='").append(timea).append("'").toString();
			rs = finance_db.executeQuery(sql);
			if (rs.next())
				account_period = rs.getString("account_period");
			for (int j = 0; j < count; j++)
				if (details_tag[j] != null)
				{
					sql = (new StringBuilder("select last_year_balance_sum from finance_gl where account_period='")).append(account_period).append("' and file_id='").append(file_id[j]).append("'").toString();
					rs = finance_db.executeQuery(sql);
					if (rs.next())
						last_balance_sum[j] = rs.getDouble("last_year_balance_sum");
					else
						last_balance_sum[j] = 0.0D;
				}

			double debit_loan = 0.0D;
			for (int j = 0; j < count; j++)
				if (details_tag[j] != null && details_tag[j].equals("0"))
				{
					debit_loan = 0.0D;
					sql = (new StringBuilder("select debit_subtotal,loan_subtotal from finance_voucher where chain_ID='")).append(file_id[j]).append("' and check_tag='1' and account_tag='1' and register_time<'").append(timea).append("' and account_period='").append(account_period).append("' order by register_time,voucher_in_month_ID").toString();
					for (rs = finance_db.executeQuery(sql); rs.next();)
					{
						last_balance_sum[j] = (last_balance_sum[j] + rs.getDouble("debit_subtotal")) - rs.getDouble("loan_subtotal");
						debit_loan = (debit_loan + rs.getDouble("debit_subtotal")) - rs.getDouble("loan_subtotal");
					}

					last_balance_sum_all += last_balance_sum[j];
					current_balance_sum[j] = last_balance_sum[j];
					setParentLastBalance(parent_category_id[j], debit_loan);
				}

			for (int j = 0; j < count; j++)
				if (details_tag[j] != null && details_tag[j].equals("0"))
				{
					sql = (new StringBuilder("select * from finance_voucher where chain_ID='")).append(file_id[j]).append("' and check_tag='1' and account_tag='1' and register_time='").append(timea).append("' order by voucher_in_month_ID").toString();
					for (rs = finance_db.executeQuery(sql); rs.next();)
					{
						debit_sum[j] += rs.getDouble("debit_subtotal");
						loan_sum[j] += rs.getDouble("loan_subtotal");
						if (rs.getDouble("debit_subtotal") != 0.0D)
							debit_count[j]++;
						else
							loan_count[j]++;
					}

					current_balance_sum[j] = (current_balance_sum[j] + debit_sum[j]) - loan_sum[j];
					setParentBalance(parent_category_id[j], debit_sum[j] - loan_sum[j]);
					setParentDebit(parent_category_id[j], debit_sum[j]);
					setParentLoan(parent_category_id[j], loan_sum[j]);
					setParentDebitCount(parent_category_id[j], debit_count[j]);
					setParentLoanCount(parent_category_id[j], loan_count[j]);
					current_balance_sum_all += current_balance_sum[j];
					debit_sum_all += debit_sum[j];
					loan_sum_all += loan_sum[j];
					debit_count_all += debit_count[j];
					loan_count_all += loan_count[j];
				}

		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public void setParentLastBalance(int parent, double value)
	{
		last_balance_sum[parent] += value;
		current_balance_sum[parent] = last_balance_sum[parent];
		if (parent_category_id[parent] != 0)
			setParentLastBalance(parent_category_id[parent], value);
	}

	public void setParentBalance(int parent, double value)
	{
		current_balance_sum[parent] += value;
		if (parent_category_id[parent] != 0)
			setParentBalance(parent_category_id[parent], value);
	}

	public void setParentDebit(int parent, double value)
	{
		debit_sum[parent] += value;
		if (parent_category_id[parent] != 0)
			setParentDebit(parent_category_id[parent], value);
	}

	public void setParentLoan(int parent, double value)
	{
		loan_sum[parent] += value;
		if (parent_category_id[parent] != 0)
			setParentLoan(parent_category_id[parent], value);
	}

	public void setParentDebitCount(int parent, int value)
	{
		debit_count[parent] += value;
		if (parent_category_id[parent] != 0)
			setParentDebitCount(parent_category_id[parent], value);
	}

	public void setParentLoanCount(int parent, int value)
	{
		loan_count[parent] += value;
		if (parent_category_id[parent] != 0)
			setParentLoanCount(parent_category_id[parent], value);
	}

	public int[] getCategory_id()
	{
		return category_id;
	}

	public void setCategory_id(int category_id[])
	{
		this.category_id = category_id;
	}

	public int getCount()
	{
		return count;
	}

	public void setCount(int count)
	{
		this.count = count;
	}

	public double[] getCurrent_balance_sum()
	{
		return current_balance_sum;
	}

	public void setCurrent_balance_sum(double current_balance_sum[])
	{
		this.current_balance_sum = current_balance_sum;
	}

	public double getCurrent_balance_sum_all()
	{
		return current_balance_sum_all;
	}

	public void setCurrent_balance_sum_all(double current_balance_sum_all)
	{
		this.current_balance_sum_all = current_balance_sum_all;
	}

	public int[] getDebit_count()
	{
		return debit_count;
	}

	public void setDebit_count(int debit_count[])
	{
		this.debit_count = debit_count;
	}

	public int getDebit_count_all()
	{
		return debit_count_all;
	}

	public void setDebit_count_all(int debit_count_all)
	{
		this.debit_count_all = debit_count_all;
	}

	public double[] getDebit_sum()
	{
		return debit_sum;
	}

	public void setDebit_sum(double debit_sum[])
	{
		this.debit_sum = debit_sum;
	}

	public double getDebit_sum_all()
	{
		return debit_sum_all;
	}

	public void setDebit_sum_all(double debit_sum_all)
	{
		this.debit_sum_all = debit_sum_all;
	}

	public String[] getDetails_tag()
	{
		return details_tag;
	}

	public void setDetails_tag(String details_tag[])
	{
		this.details_tag = details_tag;
	}

	public String[] getFile_id()
	{
		return file_id;
	}

	public void setFile_id(String file_id[])
	{
		this.file_id = file_id;
	}

	public String[] getFile_name()
	{
		return file_name;
	}

	public void setFile_name(String file_name[])
	{
		this.file_name = file_name;
	}

	public double[] getLast_balance_sum()
	{
		return last_balance_sum;
	}

	public void setLast_balance_sum(double last_balance_sum[])
	{
		this.last_balance_sum = last_balance_sum;
	}

	public double getLast_balance_sum_all()
	{
		return last_balance_sum_all;
	}

	public void setLast_balance_sum_all(double last_balance_sum_all)
	{
		this.last_balance_sum_all = last_balance_sum_all;
	}

	public int[] getLoan_count()
	{
		return loan_count;
	}

	public void setLoan_count(int loan_count[])
	{
		this.loan_count = loan_count;
	}

	public int getLoan_count_all()
	{
		return loan_count_all;
	}

	public void setLoan_count_all(int loan_count_all)
	{
		this.loan_count_all = loan_count_all;
	}

	public double[] getLoan_sum()
	{
		return loan_sum;
	}

	public void setLoan_sum(double loan_sum[])
	{
		this.loan_sum = loan_sum;
	}

	public double getLoan_sum_all()
	{
		return loan_sum_all;
	}

	public void setLoan_sum_all(double loan_sum_all)
	{
		this.loan_sum_all = loan_sum_all;
	}

	public int[] getParent_category_id()
	{
		return parent_category_id;
	}

	public void setParent_category_id(int parent_category_id[])
	{
		this.parent_category_id = parent_category_id;
	}

	public int getReal_count()
	{
		return real_count;
	}

	public void setReal_count(int real_count)
	{
		this.real_count = real_count;
	}

	public int[] getResult()
	{
		return result;
	}

	public void setResult(int result[])
	{
		this.result = result;
	}
}
